
local L_GOLD = "g"
local L_SILVER = "s"
local L_COPPER = "c"

local function Print(...) 
	DEFAULT_CHAT_FRAME:AddMessage(string.join(" ", "|cFF33FF99qRepair|r:", ...)) 
end

local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event, ...) if self[event] then return self[event](self, event, ...) end end)

function f:PLAYER_LOGIN()
	LibStub("tekKonfig-AboutPanel").new(nil, "qRepair")
	self:RegisterEvent("MERCHANT_SHOW")
end

function f:MERCHANT_SHOW()
	if CanMerchantRepair() then
		local cost = GetRepairAllCost()
		local gold = floor(cost / 10000)
		local silver = floor((cost - (gold * 10000)) / 100)
		local copper = mod(cost, 100)
		local formattedCost = format("%i|cffffd700%s|r %i|cffc7c7cf%s|r %i|cffeda55f%s|r", gold, L_GOLD, silver, L_SILVER, copper, L_COPPER)

		if GetMoney() > cost and cost > 0 then
			RepairAllItems()
			DEFAULT_CHAT_FRAME:AddMessage("Total repair: " .. formattedCost)
		end
	end
end

if MerchantFrame:IsVisible() then f:MERCHANT_SHOW() end
if IsLoggedIn() then f:PLAYER_LOGIN() else f:RegisterEvent("PLAYER_LOGIN") end
